package com.bky.controller;

import com.bky.model.Add;
import com.bky.mq.producer.AddProducer;
import com.bky.service.AddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BaseController {

    @Autowired
	private AddService addService;
	@Autowired
	private AddProducer addProducer;

	@RequestMapping("addInfo")
	public String add(Add add,HttpServletRequest request){
		try {
			System.out.println(":::::" + add.getTname() + ":::::" + add.getTpwd());
			String str = addService.addInfo(add);
			request.setAttribute("InfoMessage", str);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "添加信息失败！具体异常信息：" + e.getMessage());
		} finally {			
			return "result";
		}
	}
	
	@RequestMapping(value = "getAll")
	public String getAll(HttpServletRequest request){
		try {
			List<Add> list = addService.getAll();
			request.setAttribute("addLists", list);
			return "listAll";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "信息载入失败！具体异常信息：" + e.getMessage());
			return "result";
		}
	}
	@RequestMapping("del")
	public String del(Integer tid,HttpServletRequest request){
		try {			
			String str = addService.delete(tid);
			request.setAttribute("InfoMessage", str);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "删除信息失败！具体异常信息：" + e.getMessage());
		} finally {			
			return "result";
		}
	}
	@RequestMapping("modify")
	public String modify(Integer tid,HttpServletRequest request){
		try {
			Add add = addService.findById(tid);
			request.setAttribute("add", add);
			return "modify";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "信息载入失败！具体异常信息：" + e.getMessage());
			return "result";
		}
	}
	@RequestMapping("update")
	public String update(Add add,HttpServletRequest request){
		try {			
			String str = addService.update(add);
			addProducer.asynSend(add);
			request.setAttribute("InfoMessage", str);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "更新信息失败！具体异常信息：" + e.getMessage());
		} finally {			
			return "result";
		}
	}

}
